package com.heaboy.feedback.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heaboy.databind.binding.Binder;
import com.heaboy.feedback.constant.EnumLessonStatus;
import com.heaboy.feedback.domain.*;
import com.heaboy.feedback.domain.vo.CourseStudentVO;
import com.heaboy.feedback.domain.vo.StudentClazzVO;
import com.heaboy.feedback.domain.vo.StudentInfoVO;
import com.heaboy.feedback.service.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.heaboy.feedback.mapper.StudentInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 学生Service业务层处理
 *
 * @author heaboy
 * @date 2024-02-01
 */
@Service
public class StudentInfoServiceImpl extends ServiceImpl<StudentInfoMapper, StudentInfo> implements IStudentInfoService {
    @Autowired
    private StudentInfoMapper studentInfoMapper;

    @Autowired
    private IStudentClazzService studentClazzService;
    @Autowired
    private ICourseStudentService courseStudentService;

    @Autowired
    private ICourseClazzService courseClazzService;
    @Autowired
    private ILessonService lessonService;

    /**
     * 查询学生
     *
     * @param id 学生主键
     * @return 学生
     */
    @Override
    public StudentInfo selectStudentInfoById(Long id) {
        return studentInfoMapper.selectStudentInfoById(id);
    }

    /**
     * 查询学生列表
     *
     * @param studentInfo 学生
     * @return 学生
     */
    @Override
    public List<StudentInfo> selectStudentInfoList(StudentInfo studentInfo) {
        return studentInfoMapper.selectStudentInfoList(studentInfo);
    }

    /**
     * 新增学生
     *
     * @param studentInfo 学生
     * @return 结果
     */
    @Override
    public int insertStudentInfo(StudentInfo studentInfo) {
        studentInfo.setCreateTime(DateUtils.getNowDate());
        return studentInfoMapper.insertStudentInfo(studentInfo);
    }

    /**
     * 修改学生
     *
     * @param studentInfo 学生
     * @return 结果
     */
    @Override
    public int updateStudentInfo(StudentInfo studentInfo) {
        studentInfo.setUpdateTime(DateUtils.getNowDate());
        return studentInfoMapper.updateStudentInfo(studentInfo);
    }

    /**
     * 批量删除学生
     *
     * @param ids 需要删除的学生主键
     * @return 结果
     */
    @Override
    public int deleteStudentInfoByIds(Long[] ids) {
        return studentInfoMapper.deleteStudentInfoByIds(ids);
    }

    /**
     * 删除学生信息
     *
     * @param id 学生主键
     * @return 结果
     */
    @Override
    public int deleteStudentInfoById(Long id) {
        return studentInfoMapper.deleteStudentInfoById(id);
    }

    @Override
    public StudentInfoVO listMyInfo() {
        //todo  查询已经加入的班级 查询已经 加入的课程
        Long userId = SecurityUtils.getUserId();
        StudentInfo studentInfo = studentInfoMapper.selectStudentInfoById(userId);
        StudentInfoVO studentInfoVO = new StudentInfoVO();
        if (studentInfo != null) {
            BeanUtils.copyBeanProp(studentInfoVO, studentInfo);
        }
        //查询绑定课程
        List<CourseStudent> courseStudents = courseStudentService.list(new LambdaUpdateWrapper<CourseStudent>().eq(CourseStudent::getStudentId, userId));
        List<CourseStudentVO> courseStudentVOS = Binder.convertAndBindRelations(courseStudents, CourseStudentVO.class);
        studentInfoVO.setCourseStudentVOList(courseStudentVOS);
        //查询绑定班级
        List<StudentClazz> studentClazzes = studentClazzService.list(new LambdaUpdateWrapper<StudentClazz>().eq(StudentClazz::getStudentId, userId));
        List<StudentClazzVO> studentClazzVOS = Binder.convertAndBindRelations(studentClazzes, StudentClazzVO.class);
        studentInfoVO.setStudentClazzVOList(studentClazzVOS);
        //查询正在上的课程
        List<CourseClazz> courseClazzes = courseClazzService.list(
                new LambdaQueryWrapper<CourseClazz>()
                .in(CourseClazz::getClazzId, studentClazzes.stream().map(StudentClazz::getClazzId).collect(Collectors.toList()))
        );
        List<Lesson> lessonList = lessonService.list(new LambdaQueryWrapper<Lesson>().eq(Lesson::getStatus, EnumLessonStatus.START.getCode()).in(Lesson::getCourseClazzId, courseClazzes.stream().map(CourseClazz::getId).collect(Collectors.toList())));
        studentInfoVO.setLessonList(lessonList);
        return studentInfoVO;
    }
}
